package github.kawaiior.yuanchuxiuzhen.common.item.misc;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.block.ore.ManaStoneOre;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.world.dimension.YcxzTeleporter;
import github.kawaiior.yuanchuxiuzhen.core.init.DimensionInit;
import github.kawaiior.yuanchuxiuzhen.core.init.itemgroup.YcxzGroup;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import net.minecraft.block.BeaconBlock;
import net.minecraft.block.BlockState;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.util.LazyOptional;

import javax.annotation.Nullable;
import java.util.List;

public class VitalityStone extends Item {
    public VitalityStone() {
        super(new Properties().group(YcxzGroup.YCXZ_GROUP).maxStackSize(1));
    }

    private void updatePlayerDim(World worldIn, PlayerEntity player, BlockPos pos){
        MinecraftServer server = worldIn.getServer();
        ServerWorld world;
        boolean flag = false;
        if (worldIn.getDimensionKey() == DimensionInit.HELL){
            world = server.getWorld(World.OVERWORLD);
            flag = true;
        }else if (worldIn.getDimensionKey() == World.THE_NETHER){
            world = server.getWorld(DimensionInit.HELL);
        }else {
            return;
        }

        if (world == null) {
            return;
        }

        player.changeDimension(world, new YcxzTeleporter(pos, flag));
    }

    @Override
    public ActionResultType onItemUse(ItemUseContext context) {
        World world = context.getWorld();
        if (!world.isRemote) {
            PlayerEntity player = context.getPlayer();
            BlockPos pos = context.getPos();
            BlockState state = world.getBlockState(pos);
            if (state.getBlock() instanceof BeaconBlock){
                updatePlayerDim(world, player, pos);
            }else {
                this.onItemRightClick(world, player, context.getHand());
            }
        }
        return ActionResultType.func_233537_a_(world.isRemote);
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if (!playerIn.isSneaking()){
            return super.onItemRightClick(worldIn, playerIn, handIn);
        }

        if (worldIn.isRemote){
            return super.onItemRightClick(worldIn, playerIn, handIn);
        }

        ItemStack itemstack = playerIn.getHeldItem(handIn);
        if (itemstack.getItem() instanceof VitalityStone){
            LazyOptional<ManaPower> capability = playerIn.getCapability(ModCapability.MANA_POWER);
            capability.ifPresent((power)->{
                if (power.isVitality()){
                    // TODO: use i18
                    playerIn.sendMessage(new StringTextComponent("无法二次使用"), playerIn.getUniqueID());
                }else {
                    power.setVitality(true);
                    itemstack.shrink(1);
                }
            });
        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    @Override
    public void addInformation(ItemStack stack, @Nullable World world, List<ITextComponent> list, ITooltipFlag flag) {
        super.addInformation(stack, world, list, flag);
        list.add(new TranslationTextComponent("item."+Yuanchuxiuzhen.MODID+"."+this.getRegistryName().getPath()+".desc"));
    }
}
